﻿//===============================================================================
// SharePoint911 - http://www.SharePoint911.com
// SharePoint ActiveX Override (SPAXO)
// Created By: Larry J. Riemann
//===============================================================================
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
//===============================================================================

using System;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics;
using System.IO;

namespace SharePoint911.SPAXO.Logging
{
    /// <summary>
    /// Wrapper for the TraceProvider class that makes the registering and unregistering of the TraceProvider more straight forward by using the disposable object pattern
    /// </summary>
    public class UlsLogWriter : IDisposable
    {
        private bool _isDisposed;
        private Guid _correlationId;

        public UlsLogWriter()
        {
            // Register the trace provider
            TraceProvider.RegisterTraceProvider();
            _correlationId = Guid.NewGuid();
        }

        #region IDisposable Members

        /// <summary>
        /// Implement the Dispose method of the IDisposable interface
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Required for the dispose pattern
        /// </summary>
        /// <param name="disposing">True if managed object can be disposed</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                if (disposing)
                {
                    // Dispose of managed resources here (if any)
                }

                TraceProvider.UnregisterTraceProvider();

                _isDisposed = true;
            }
        }

        /// <summary>
        /// Finalizer
        /// </summary>
        ~UlsLogWriter()
        {
            Dispose(false);
        }

        #endregion

        /// <summary>
        /// Writes Information to the ULS Log with the name of the process and assembly defaults
        /// </summary>
        /// <param name="level">Severity of the Trace item</param>
        /// <param name="categoryName">Category of information</param>
        /// <param name="message">Message to record to the log</param>
        [CLSCompliant(false)]
        public void Write(TraceProvider.TraceSeverity level, string categoryName, string message)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            else
            {
                TraceProvider.WriteTrace(0, level, _correlationId, new FileInfo(Process.GetCurrentProcess().MainModule.FileName).Name, "SharePoint911.SPAXO", categoryName, message);
            }
        }
    }
}
